-
Notifications
You must be signed in to change notification settings - Fork 5
/
Linked list.cpp
70 lines (61 loc) · 1.37 KB
/
Linked list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
using namespace std;
struct node{
int data;
struct node *next;
};
node *start=NULL;
node *createNode(){
node *temp;
temp = new node;
return temp;
}
void insertNode(){
node *temp,*traverse;
int n;
cout<<"Enter -1 to End"<<endl;
cout<<"Enter the values to be added in List"<<endl;
cin>>n;
while( n!=-1){
temp = createNode();
temp->data = n;
temp->next = NULL;
if(start == NULL){
start = temp;
}
else{
traverse = start;
while(traverse->next != NULL){
traverse= traverse->next;
}
traverse->next= temp;
}
cout<<"Enter the value to be added in the list"<<endl;
cin>>n;
}
}
void printlist(){
node *traverse = start;
while(traverse != NULL){
cout<<traverse->data<<" ";
traverse = traverse->next;
}
}
int main(){
int option;
do{
cout<<"---------------Main Menu-------------------"<<endl;
cout<<"1 :Create List"<<endl;
cout<<"2. Print List"<<endl;
cout<<"3. Exit"<<endl;
cin>>option;
switch(option){
case 1:insertNode();
break;
case 2:printlist();
break;
}
}while(option != 3);
}
/*Don't forget to watch My Video on How to code Doubly linked list , Once You Understand Doubly Linked List then
you can code any program using LinkedList by yourself*/